package com.ruoyi.erp.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.erp.domain.LedyFeeCheckin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.erp.mapper.LedyTaxMapper;
import com.ruoyi.erp.domain.LedyTax;
import com.ruoyi.erp.service.ILedyTaxService;

/**
 * 开票费用凭证Service业务层处理
 *
 * @author ztj
 * @date 2023-07-05
 */
@Service
public class LedyTaxServiceImpl implements ILedyTaxService {
    @Autowired
    private LedyTaxMapper ledyTaxMapper;

    /**
     * 查询开票费用凭证
     *
     * @param id 开票费用凭证主键
     * @return 开票费用凭证
     */
    @Override
    public LedyTax selectLedyTaxById(Long id) {
        return ledyTaxMapper.selectLedyTaxById(id);
    }

    @Override
    public List<LedyTax> selectLedyTaxByIds(Long[] ids) {
        return ledyTaxMapper.selectLedyTaxByIds(ids);
    }

    /**
     * 查询开票费用凭证列表
     *
     * @param ledyTax 开票费用凭证
     * @return 开票费用凭证
     */
    @Override
    public List<LedyTax> selectLedyTaxList(LedyTax ledyTax) {
        return ledyTaxMapper.selectLedyTaxList(ledyTax);
    }

    @Override
    public String importTax(List<LedyTax> list, Boolean isUpdateSupport) {
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (LedyTax tax : list) {
            try {
                // 验证是否存在
                LedyTax ledyTax = new LedyTax();
//                ledyTax.setTaxRate(ledyTax.getTaxAmount().divide(ledyTax.getSumMoney()));
                if (StringUtils.isEmpty(tax.getRecordNumber())) {
                    ledyTax.setRecordNumber(tax.getNumNumber());
                    tax.setRecordNumber(tax.getNumNumber());
                } else
                    ledyTax.setRecordNumber(tax.getRecordNumber());
                ledyTax.setGoodsName(tax.getGoodsName());
                List<LedyTax> ledyTaxList = ledyTaxMapper.selectLedyTaxList(ledyTax);
                if (ledyTaxList.size() == 0) {
                    this.insertLedyTax(tax);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、发票" + tax.getRecordNumber() + " 导入成功");
                } else if (isUpdateSupport) {
                    tax.setId(ledyTaxList.get(0).getId());
                    this.updateLedyTax(tax);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、发票 " + tax.getRecordNumber() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、发票 " + tax.getRecordNumber() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + tax.getRecordNumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 新增开票费用凭证
     *
     * @param ledyTax 开票费用凭证
     * @return 结果
     */
    @Override
    public int insertLedyTax(LedyTax ledyTax) {
        ledyTax.setCreateTime(DateUtils.getNowDate());
        return ledyTaxMapper.insertLedyTax(ledyTax);
    }

    /**
     * 修改开票费用凭证
     *
     * @param ledyTax 开票费用凭证
     * @return 结果
     */
    @Override
    public int updateLedyTax(LedyTax ledyTax) {
        ledyTax.setUpdateTime(DateUtils.getNowDate());
        return ledyTaxMapper.updateLedyTax(ledyTax);
    }

    /**
     * 批量删除开票费用凭证
     *
     * @param ids 需要删除的开票费用凭证主键
     * @return 结果
     */
    @Override
    public int deleteLedyTaxByIds(Long[] ids) {
        return ledyTaxMapper.deleteLedyTaxByIds(ids);
    }

    /**
     * 删除开票费用凭证信息
     *
     * @param id 开票费用凭证主键
     * @return 结果
     */
    @Override
    public int deleteLedyTaxById(Long id) {
        return ledyTaxMapper.deleteLedyTaxById(id);
    }
}
